package com.lizard.schedule.manager.umeng;

import android.app.Activity;
import android.content.Intent;

import com.lizard.schedule.ScheduleApplication;
import com.lizard.schedule.R;
import com.lizard.schedule.log.ScheduleLog;
import com.lizard.schedule.manager.qq.QQConst;
import com.lizard.schedule.manager.wx.WxConst;
import com.lizard.schedule.net.http.consts.HttpConst;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.SmsHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.utils.Log;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

/**
 * 友盟分享管理类
 */
public class UmengShareManager {

    private static final String TAG = UmengShareManager.class.getSimpleName();

    private static UmengShareManager instance;

    /**
     * 分享标题
     */
    private String mShareTitle;

    /**
     * 分享的文本内容
     */
    public String mShareText;

    /**
     * 分享到新浪微博的文本内容
     */
    public String mShareSinaText;

    /**
     * 分享的链接
     */
    public static String mShareLink;

    /**
     * 分享的icon(小)
     */
    public static int mShareIconSmall;

    /**
     * 分享的icon(大)
     */
    public static int mShareIconLarge;

    private UMSocialService mController;

    private UmengShareManager(){

        initShareInfo();

        Log.LOG = true;

        mController = UMServiceFactory.getUMSocialService("com.umeng.share");
        mController.getConfig().closeToast();

        // 设置新浪SSO handler
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
    }

    /**
     * 初始化分享的内容信息
     */
    private void initShareInfo(){
        mShareTitle = ScheduleApplication.applicationContext.getString(R.string.share_content_title);
        mShareText = ScheduleApplication.applicationContext.getString(R.string.share_content_text);
        mShareSinaText = ScheduleApplication.applicationContext.getString(R.string.share_sina_content_text);
        mShareLink = HttpConst.SHARE_LINK;
        mShareIconSmall = R.mipmap.share_icon_small;
        mShareIconLarge = R.mipmap.share_icon_large;
    }

    public static UmengShareManager getInstance(){
        if(instance == null){
            synchronized (UmengShareManager.class){
                if(instance == null){
                    instance = new UmengShareManager();
                }
            }
        }
        return instance;
    }

    public String getShareLink(){
        return mShareLink;
    }

    /**
     * sina在对应的activity的onActivityResult中调用
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void sinaSsoCallBack(int requestCode, int resultCode, Intent data){
        UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode) ;
        if(ssoHandler != null){
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }

    /**
     * 分享到短信
     *
     * @param activity
     */
    public void shareSms(Activity activity){

        // 短信 handler
        SmsHandler smsHandler = new SmsHandler();
        smsHandler.addToSocialSDK();

        mController.setShareContent(mShareSinaText + mShareLink);

        share(activity, SHARE_MEDIA.SMS);
    }

    /**
     * 分享到新浪微博
     *
     * @param activity
     */
    public void shareSina(Activity activity){
        SinaShareContent sinaShareContent = new SinaShareContent();
        sinaShareContent.setShareContent(mShareSinaText + mShareLink);
        sinaShareContent.setShareImage(new UMImage(activity, mShareIconLarge));

        mController.setShareMedia(sinaShareContent);

        share(activity, SHARE_MEDIA.SINA);
    }

    /**
     * 分享到qq好友
     *
     * @param activity
     */
    public void shareQQFriend(Activity activity){
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(activity, QQConst.APP_ID, QQConst.APP_KEY);
        qqSsoHandler.addToSocialSDK();

        QQShareContent qqShareContent = new QQShareContent();
        qqShareContent.setShareContent(mShareText);
        qqShareContent.setTitle(mShareTitle);
        qqShareContent.setTargetUrl(mShareLink);
        qqShareContent.setShareImage(new UMImage(activity, mShareIconSmall));

        mController.setShareMedia(qqShareContent);

        share(activity, SHARE_MEDIA.QQ);

    }

    /**
     * 分享到qq空间
     *
     * @param activity
     */
    public void shareQZone(Activity activity){
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(activity, QQConst.APP_ID, QQConst.APP_KEY);
        qZoneSsoHandler.addToSocialSDK();

        QZoneShareContent qzone = new QZoneShareContent();
        qzone.setShareContent(mShareText);
        qzone.setTargetUrl(mShareLink);
        qzone.setTitle(mShareTitle);
        qzone.setShareImage(new UMImage(activity, mShareIconLarge)); // 分享到QQ空间的图片如果太小 QQ空间会显示不出来

        mController.setShareMedia(qzone);

        share(activity, SHARE_MEDIA.QZONE);

    }

    /**
     * 分享到微信好友
     *
     * @param activity
     */
    public void shareWXFriend(Activity activity){
        UMWXHandler wxHandler = new UMWXHandler(activity, WxConst.WX_APP_ID,WxConst.WX_APP_SECRET);
        wxHandler.addToSocialSDK();

        WeiXinShareContent weixinContent = new WeiXinShareContent();
        weixinContent.setShareContent(mShareText);
        weixinContent.setTitle(mShareTitle);
        weixinContent.setTargetUrl(mShareLink);
        weixinContent.setShareImage(new UMImage(activity, mShareIconSmall));

        mController.setShareMedia(weixinContent);

        share(activity, SHARE_MEDIA.WEIXIN);
    }

    /**
     * 分享到微信朋友圈
     *
     * @param activity
     */
    public void shareWXCircle(Activity activity){
        UMWXHandler wxHandler = new UMWXHandler(activity, WxConst.WX_APP_ID, WxConst.WX_APP_SECRET);
        wxHandler.setToCircle(true);
        wxHandler.addToSocialSDK();

        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setShareContent(mShareText);
        circleMedia.setTitle(mShareTitle);
        circleMedia.setTargetUrl(mShareLink);
        circleMedia.setShareImage(new UMImage(activity, mShareIconSmall));

        mController.setShareMedia(circleMedia);

        share(activity, SHARE_MEDIA.WEIXIN_CIRCLE);
    }

    private void share(Activity activity, SHARE_MEDIA type){
        mController.postShare(activity, type,
                new SocializeListeners.SnsPostListener() {
                    @Override
                    public void onStart() {
                        ScheduleLog.v(TAG, "开始分享");
                    }

                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int eCode, SocializeEntity socializeEntity) {
                        if (eCode == 200) {
                            ScheduleLog.v(TAG, "分享成功");
                        } else {
                            String eMsg = "";
                            if (eCode == -101){
                                eMsg = "没有授权";
                            }
                            ScheduleLog.v(TAG, "分享失败[" + eCode + "] " + eMsg);
                        }
                    }
                });
    }

}
